package org.qydjk.h5.filter;

import org.qydjk.common.constant.CommonConstant;
import org.qydjk.common.pojo.Area;
import org.qydjk.common.util.CookieUtil;
import org.qydjk.common.util.SpringContextUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.service.service.area.IAreaService;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * 定位过滤器
 * 1、页面上城市区域的名字从session中取
 * 2、城市区域信息初始化到redis里面
 */
public class LocationFilter implements Filter{
    private static IAreaService areaService;

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        HttpSession session = request.getSession();

        String sku = CookieUtil.getCooklieValue(request, "sku");
        if(!StringUtil.isEmptyOrNull(sku)){
            sku = URLDecoder.decode(sku, "utf-8");
            sku = StringUtil.decodeUnicode(sku);
            System.out.println(sku);
            filterChain.doFilter(request, response);
            return;
        }

        //1、经纬度处理
        String sessionLoc = (String) session.getAttribute(CommonConstant.COMMON_LON_LAT_SESSION_NAME);
        String cookieLoc = CookieUtil.getCooklieValue(request, CommonConstant.COMMON_LON_LAT_COOKIE_NAME);
        if(!StringUtil.isEmptyOrNull(cookieLoc) && !StringUtil.isEmptyOrNull(sessionLoc)){
            //session和cookie都有，如果不同，则将cookie里面的位置写入session
            if(!cookieLoc.equals(sessionLoc)){
                //TODO 判断地址是否有效然后写入
                session.setAttribute(CommonConstant.COMMON_LON_LAT_SESSION_NAME,cookieLoc);
            }

        }else if(!StringUtil.isEmptyOrNull(cookieLoc) && StringUtil.isEmptyOrNull(sessionLoc)){
            //TODO 判断地址是否有效然后写入
            //cookie有session没有，写入session
            session.setAttribute(CommonConstant.COMMON_LON_LAT_SESSION_NAME,cookieLoc);
        }

        //2、城市区域处理
        //从session里面获取城市区域code
        String sessionAreaCode = (String) session.getAttribute(CommonConstant.COMMON_AREA_CODE_SESSION_NAME);
        //从cookie里面获取城市区域code
        String cookieAreaCode = CookieUtil.getCooklieValue(request, CommonConstant.COMMON_AREA_COOKIE_NAME);
        try{
            if(areaService == null){
                areaService = (IAreaService) SpringContextUtil.getBean("areaService");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        if(StringUtil.isEmptyOrNull(sessionAreaCode) && StringUtil.isEmptyOrNull(cookieAreaCode)){
            //session和cookie都没有，设置默认城市
            //放入cookie
            CookieUtil.addCookie(response,CommonConstant.COMMON_AREA_COOKIE_NAME,CommonConstant.COMMON_DEFAULT_AREA_CODE);
            putToSession(CommonConstant.COMMON_DEFAULT_AREA_CODE,session);
        }else if(StringUtil.isEmptyOrNull(sessionAreaCode) && !StringUtil.isEmptyOrNull(cookieAreaCode)){
            //session没有cookie有，将cookie里的设置到session
            //cookie区域放入session
            putToSession(cookieAreaCode,session);
        }else if(!StringUtil.isEmptyOrNull(sessionAreaCode) && !StringUtil.isEmptyOrNull(cookieAreaCode)){
            //session和cookie都有，判断是否相同，如果不同，则以cookie为准
            if(!sessionAreaCode.equals(cookieAreaCode)){
                //cookie区域放入session
                putToSession(cookieAreaCode,session);
            }
        }
        filterChain.doFilter(request, response);
    }

    private void putToSession(String code,HttpSession session){
        //cookie区域放入session
        try {
            Area area = areaService.getAreaByCode(code);
            if(area != null){
                //放入session
                session.setAttribute(CommonConstant.COMMON_AREA_ID_SESSION_NAME,area.getId());
                session.setAttribute(CommonConstant.COMMON_AREA_CODE_SESSION_NAME,code);
                session.setAttribute(CommonConstant.COMMON_AREA_SESSION_NAME,area.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void destroy() {

    }
}
